package com.kkb.servlet;

import com.kkb.pojo.Customer;
import com.kkb.service.CustomerService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;

@WebServlet("/addCustomer")
public class CustomerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Customer customer = new Customer();
        Map<String, String[]> parameterMap = request.getParameterMap();
        try {
            BeanUtils.populate(customer,parameterMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        CustomerService customerService = new CustomerService();
        try {
            customerService.addCustomer(customer);
            response.sendRedirect(request.getContextPath() + "index.jsp");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
